function appUrl(url) {
    let http = "http://localhost:8080";
    return http + url;
}

/**
 * 对返回结果进行处理
 *
 * @param json
 * @param callback
 */
function handlerRead(json, callback) {
    let parse = JSON.parse(json);
    if (parse.code === 100) {
        //成功执行回调函数
        callback(parse.data);
    } else if(parse.code === 300){
        alert("用户未登录，无法进入管里后台");
        location.href = "http://localhost:8080/view_admin/login.html";
    } else if (parse.code === 301) {
        alert("用户未登录，无法进入个人中心");
        location.href = "http://localhost:8080/view_api/loginpass.html";
    } else {
        alert(parse.msg);
    }
}

/**
 * doGet请求无参
 * @param url
 * @param callback
 */
function doGet(url, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "get",
        url: url1,
        success: function (resp) {
            //这里可能有点绕，仔细看
            handlerRead(resp, callback);
        }
    });
}

/**
 * get请求带参
 * @param url
 * @param callback
 * @param json
 */
function getWithParams(url, params, callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "get",
        url: url1,
        data: params,
        success: function (resp) {
            handlerRead(resp, callback)
        }
    })
}

/**
 * post请求带参,转json
 * @param url
 * @param callback
 * @param json
 */
function doPostWithJson(url, json, callback) {
    let json1 = JSON.stringify(json);
    let url1 = appUrl(url);
    $.ajax({
        type: "post",
        url: url1,
        data: json1,
        success: function (resp) {
            handlerRead(resp, callback);
        }
    });
}

/**
 * post请求带参(不转json)
 * @param url
 * @param callback
 * @param json
 */
function doPost(url,params,callback) {
    let url1 = appUrl(url);
    $.ajax({
        type: "post",
        url: url1,
        data: params,
        success: function (resp) {
            console.log("拿到异常信息啦");
            console.log(resp);
            handlerRead(resp, callback)
        }
    });
}


